echo off
setlocal enableextensions enabledelayedexpansion
echo =======================================================
echo ePSXe Settings Script
echo =======================================================
:: ======================================================
:: ePSXe Settings Importer/Exporter
:: By Stigzler 2014
:: Search CodePlex EmVoy
:: Designed to be used with EmVoy utility but can be stand alone
:: Run util with parameters for game specific operation
:: Run util without parameters to grab current emu settings and save as default profile
:: ======================================================
:: Command Line Parameters - MUST BE PASSED IN THIS ORDER
:: 1 - Import or Export - = grab or populate settings from/to emulator. Values: "imp" or "exp"
:: 2 - Emulator Path - full path to Emu - ENCLOSE IN QUOTES
:: 3 - Gamename - this will be the name of the folder where settings are stored - ENCLOSE IN QUOTES
:: 4 - History - creates a history of settings - "Yes" or "No"(minus quotes)- Defaults to no if not passed
:: ======================================================
set impexp=%1
set emupath=%2
set gamename=%3
set history=%4
if not defined impexp (
		echo No command parameters, grabbing settings and writing to default...
		set impexp=imp
		set gamename=default
		set history=Yes
:ReEnter
		set /p emupath=Enter the path the ePSXe.exe here: 
		if exist !emupath!\ePSXe.exe (
				echo Correct path. Continuing with set default settings process....
			) else ( 
				echo Incorrect path, please re-enter. 
				goto :ReEnter
			)
		set emupath=!emupath!\
		echo Emu Path set as: [!emupath!]
	)
if %impexp% == def (
		echo def tag passed - grabbing settings and writing to default...
		set impexp=imp
		set gamename=default
		set history=Yes
	)
if not defined gamename call :Error "Mode set, but no emulator path passed. Can't complete script. Quitting....."
if not defined gamename call :Error "Mode set, but no gamename passed. Can't complete script. Quitting....."
if not defined history set history=No

:ValidateParams
::Passed variables validation
echo Validating passed variables:
echo Emulator Path=%emupath%
set emupath=%emupath:"=%
echo Ammended E ulator Path=%emupath%
echo Gamename=%gamename%
set gamename=%gamename:"=%
echo Ammended Gamename=%gamename%
echo Import or Export = %impexp% 
set res=False
if %impexp% == imp set res=true
if %impexp% == exp set res=true
if %res% == true (
	echo Import Export switch valid
	) else (
		echo Import/Export switch Invalid! Stopping Script.
		exit /b 
	)
		
echo Preserve History = %history%
set res=False
if %history% == Yes set res=true
if %history% == No set res=true
if %res% == true (
	echo History switch valid
	) else (
		echo History switch Invalid! Stopping Script.
		exit )
		
		
:: ========================================================
:: Global Variables
set timestamp=%DATE:/=-%@%TIME::=-%
set timestamp=%timestamp: =%
set filenamegb=default
set filenamepb=default

:: =====================================================
:: User Variables - SET HERE!

:: Path pcsxr game settings folder:
set ScriptPath=%~dp0
set ScriptPath=%ScriptPath:~0,-1%
set settpath=%ScriptPath%\epsxe
:: Set up filenames for general and plugin settings:
echo *** Constructing filenames....
set filenameg="%gamename%_general.reg"
set filenamep="%gamename%_plugins.reg"
echo *** General Settings Filename: %filenameg%
echo *** Plugin Settings Filename: %filenamep%

:: ========================================================
:: Code ----------------------------------------------
echo ====================================================================================================
Echo ** Choosing Export or Import:
If %impexp% == imp (
	echo ** Start Import Process
	call :ImportSettings
	echo ** Settings Imported from Emulator!	)
If %impexp% == exp (
	echo ** Start Export Process
	call :ExportSettings
	echo ** Export process finished!	)
echo **** Ending Game Setting process. 
::pause
exit
	
:: ======================================================
:: Subroutines

:Error [message]
echo Uh oh... Fatal Error: %~1 
echo Shutting down script
exit /b 

:ImportSettings
:: Create/set folders for game
%settpath:~0,2%
cd %settpath%
echo *** Lining up Game folder...
if exist "%gamename%" (
	echo *** Game folder already exists - selecting directory
	cd "%gamename%"
	) else (
	echo *** Creating new game folder and selecting directory
	mkdir "%gamename%"
	cd "%gamename%")

:: if history required:
echo *** User History request- history flag is "%history%"

:: Start of long if.---------------------------------------------------------
if "%history%" == "Yes" (
	:: setup folders + backup files
	echo **** User requiring backups - setup folders...
	if exist backup (
		echo **** Game settings backup folder already exists
	) else (
		echo **** Game settings backup folder does not exist - creating new backup folder
		mkdir backup )
	set filenamegb="%gamename%_general_%timestamp%.reg"
	set filenamepb="%gamename%_plugins_%timestamp%.reg"
	echo **** Setting backup filenames:
	echo **** new General Settings Backup Filename: !filenamegb!
	echo **** new Plugin Settings Backup Filename: !filenamepb!
	:: test if any file to back up and do it
		echo **** Creating timestamped folder in backup + copying files...
		mkdir "backup\%timestamp%"
		copy /y *.* backup\!timestamp!
		echo **** Files backed up.
) else (
	:: No history required
	echo **** User not requiring History )
:: End IF ----------------------------------------------------------

:: Read reg values and save to files
echo *** Creating new Settings files...
echo Current directory: %CD%
echo *** Registry settings being grabbed:
regedit /E "%filenameg:"=%" "HKEY_CURRENT_USER\Software\epsxe"
regedit /E "%filenamep:"=%" "HKEY_CURRENT_USER\Software\Vision Thing"
echo *** Reg settings grabbed and files written.
:: Copy Memory Cards
echo *** Memory cards being saved:
copy /y "%emupath%memcards\*.mcr"
echo *** Memory cards saved.
:: Copy any ini created by plugins, left in plugin folder 
echo *** Any plugin inis being saved:
copy /y "%emupath%plugins\*.ini"
echo *** Reg plugin inis saved.
echo *** Ending Import Settings Routine
GOTO :EOF

:ExportSettings
Echo Loading game settings into registry
%settpath:~0,2%
cd %settpath%
echo Check whether folder exists (if doesn't - first run)
if exist "%gamename%" (
		echo Folder Exists - carry on with process
	) else (
		echo Folder doesn't exist - loading defaults.
		set filenameg=default_general.reg
		set filenamep=default_plugins.reg
		set gamename=default
	)
cd "%gamename%"
echo Current directory: %CD%
echo Going onto load reg values....
::pause
regedit /S "%filenameg:"=%"
regedit /S "%filenamep:"=%"
echo Reg values loaded. Moving game memory cards and any inis to epsxe...
copy *.mcr "%emupath%memcards\"
copy *.ini "%emupath%plugins\"

::pause
GOTO :EOF
:: ========================================================

